package zmee.pmas.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.math.BigDecimal;

/**
 * @Author cb
 * @DateTime 2022/11/27 13:09
 * @Description 项目支出预算、单据编辑插件
 */
public class OutBudgetFormPlugin extends AbstractFormPlugin {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        if ("project".equals(name)) {
            // 选择项目之后给 项目概算总额 赋值
            // 项目
            DynamicObject newValue = (DynamicObject) changeData.getNewValue();
            if (null != newValue) {
                // 获取项目ID
                Object pkValue = newValue.getPkValue();
                QFilter qFilter = new QFilter("pro.id", QCP.equals, pkValue);
                qFilter.and("billstatus", QCP.equals, "C");
                // 查询 项目立项
                DynamicObject proApproval = QueryServiceHelper.queryOne("pmas_pro_approval",
                        "id,projcetbugamt", new QFilter[]{qFilter});
                if (null != proApproval) {
                    // 获取 项目投资预算总额/项目概算（元） 的值
                    BigDecimal projcetbugamt = proApproval.getBigDecimal("projcetbugamt");
                    // 项目概算总额
                    this.getModel().setValue("zmee_projcetbugamt", projcetbugamt);
                } else {
                    this.getModel().setValue("zmee_projcetbugamt", null);
                }
            } else {
                this.getModel().setValue("zmee_projcetbugamt", null);
            }
        }
    }
}